﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Contracts.Service;
using System.Linq;

namespace XcoAppSpaces.Core.Configuration
{
	/// <summary>
	/// Class for XcoService configurations.
	/// </summary>
	public class XcoServiceConfig
	{
		private string name;
		private bool isDefault;
		private readonly XcoAppSpaceConfig baseConfig;

		/// <summary>
		/// The type of the service.
		/// </summary>
		public Type ServiceType { get; set; }

		/// <summary>
		/// Mappings to other services.
		/// </summary>
		public Dictionary<Type, string> Mappings { get; private set; }

		/// <summary>
		/// The service instance.
		/// </summary>
		public IXcoService ConfiguredService { get; private set; }

		/// <summary>
		/// The name of the service.
		/// </summary>
		public string Name
		{
			get { return name; }
			set
			{
				if (value != null && baseConfig.ServiceConfigs[ServiceType].Any(cs => cs.Name == value && cs != this))
					throw new XcoConfigurationException("A service of the same type with the name " + value + " already exists.");
				name = value;
			}
		}

		/// <summary>
		/// True if the service is the default for this type of service.
		/// </summary>
		public bool IsDefault
		{
			get { return isDefault; }
			set
			{
				if (value && baseConfig.ServiceConfigs[ServiceType].Any(cs => cs.IsDefault && cs != this))
					throw new XcoConfigurationException("Another service is already defined as default.");
				isDefault = value;
			}
		}
		
		internal XcoServiceConfig(XcoAppSpaceConfig baseConfig, IXcoService service, Type serviceType)
		{
			this.Name = null;
			this.baseConfig = baseConfig;
			this.IsDefault = false;
			this.ServiceType = serviceType;
			this.ConfiguredService = service;
		}

		/// <summary>
		/// Adds a mapping to another service of the given type.
		/// </summary>
		/// <param name="serviceType">The service type.</param>
		/// <param name="serviceName">The service name.</param>
		public void AddMapping(Type serviceType, string serviceName)
		{
			if (Mappings == null)
				Mappings = new Dictionary<Type, string>();
			Mappings.Add(serviceType, serviceName);
		}

		/// <summary>
		/// Converts an XcoServiceConfig into an XcoServiceConfiguration.
		/// </summary>
		/// <param name="config">The XcoServiceConfig.</param>
		/// <returns>An XcoServiceConfiguration.</returns>
		public static implicit operator XcoServiceConfiguration(XcoServiceConfig config)
		{
			return new XcoServiceConfiguration { Service = config.ConfiguredService, Name = config.Name, IsDefault = config.IsDefault, Mappings = config.Mappings };
		}
	}
}
